﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Gerrit.Api.QuickStart
{
    public class SimpleCrawler
    {
        public event EventHandler<OnStartEventArgs> OnStart;

        public event EventHandler<OnCompletedEventArgs> OnCompleted;

        public event EventHandler<Exception> OnError;

        public CookieContainer CookieContainer { get; set; }

        public SimpleCrawler() { }

        public async Task<string> Start(Uri uri, WebProxy proxy = null)
        {
            return await Task.Run(() =>
            {
                var pageSource = string.Empty;
                try
                {
                    if (this.OnStart != null) this.OnStart(this, new OnStartEventArgs(uri));
                    var watch = new Stopwatch();
                    var request = (HttpWebRequest)WebRequest.Create(uri);
                    request.Accept = "*/*";
                    request.ContentType = "application/x-www-form-urlencoded";//定义文档类型和编码
                    request.AllowAutoRedirect = false;//禁止自动跳转
                    //设置User-Agent，伪装成Google Chream浏览器
                    //request.UserAgent = "";
                    request.Timeout = 5000;//定义请求超时时间为5秒
                    request.KeepAlive = true;//启用连接
                    request.Method = "GET";//定义请求方式为GET
                    if (proxy != null) request.Proxy = proxy;//设置代码服务器IP，伪装请求地址
                    request.CookieContainer = this.CookieContainer;//附加Cookie容器
                    request.ServicePoint.ConnectionLimit = int.MaxValue;//定义最大连接数

                    var response = (HttpWebResponse)request.GetResponse();//获取请求响应
                    foreach (Cookie cookie in response.Cookies) this.CookieContainer.Add(cookie);//将Cookie加入容器，保存登录状态
                    var stream = response.GetResponseStream();//获取响应流
                    var reader = new StreamReader(stream, Encoding.UTF8);//以UTF-8的方式读取流
                    pageSource = reader.ReadToEnd();//读取网页源码
                    watch.Stop();
                    var threadId = System.Threading.Thread.CurrentThread.ManagedThreadId;//获取当前任务线程ID
                    var milliseonds = watch.ElapsedMilliseconds;//获取请求执行时间
                    reader.Close();
                    stream.Close();
                    request.Abort();
                    response.Close();
                }
                catch (Exception ex)
                {
                    if (this.OnError != null) this.OnError(this, ex);
                }
                return pageSource;
            });
        }

    }
}
